package lagou.homework.lagou_homework_rocketmq.service.impl;

import lagou.homework.lagou_homework_rocketmq.entity.CreateOrderMessage;
import lagou.homework.lagou_homework_rocketmq.exception.SeckillException;
import lagou.homework.lagou_homework_rocketmq.repository.GoodsRepository;
import lagou.homework.lagou_homework_rocketmq.repository.SeckillRepository;
import lagou.homework.lagou_homework_rocketmq.service.SeckillService;
import lagou.homework.lagou_homework_rocketmq.utils.Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SeckillServiceImpl implements SeckillService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillRepository seckillRepository;
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 秒杀
     * @param userId
     * @param seckillId
     */
    @Override
    public String kill(String userId,String seckillId){
        /**
         * 校验
         */
        //查询队列是否存在
        Boolean isExist = redisTemplate.hasKey("seckill"+seckillId+":count" );
        if(!isExist){
            throw new SeckillException("500",""+seckillId+"秒杀不存在或已过期",null);
        }
        /**
         * 判断是否已经抢过单了
         */
        if(redisTemplate.boundSetOps("seckill"+seckillId+":users").isMember(userId)){
            throw new SeckillException("500","抱歉，"+userId+"已经抢单了，请不要重复下单",null);
        }

        /**
         * 从redis队列中弹出秒杀对象
         */
        Long decrement = redisTemplate.boundValueOps("seckill" + seckillId + ":count").decrement();
        if(decrement < 0){
            throw new SeckillException("500","抱歉"+userId+"，该产品已抢完，请下次再来",null);
        }
        /**
         * users记录
         */
        redisTemplate.boundSetOps("seckill"+seckillId+":users").add(userId);

        /**
         * 生成订单号
         */
        String orderNo = Util.uuid();

        /**
         * 发送消息，生成订单，并保存到数据库
         */
        CreateOrderMessage createOrderMessage = new CreateOrderMessage(userId,seckillId,orderNo);
       rocketMQTemplate.send("order_create", MessageBuilder.withPayload(createOrderMessage).build());
        return orderNo;
    }

    /**
     * 重置秒杀
     * @param seckillId
     */
    @Override
    public void reset(String seckillId){
        log.info("重置秒杀");
        /**
         * 数据库状态设置为0
         */
        seckillRepository.updateStatus(0,seckillId);
        /**
         * 删除redis
         */
        redisTemplate.delete("seckill"+seckillId+":count");
        redisTemplate.delete("seckill"+seckillId+":users");
        /**
         * 商品库存重置
         */
        goodsRepository.updateStock(1,10);
      
    }


}
